/**
 * Copyright (c) 2006-2009 by Shanghai UnuTrip Network Technology Development Co.,Ltd
 * All rights reserved.
 */


package com.unusap.remote.core.remote.httpinvoker;

import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
import org.springframework.remoting.httpinvoker.HttpInvokerRequestExecutor;

import com.unusap.remote.core.remote.factory.AbstractHttpWebServiceFactory;
import com.unusap.remote.core.remote.factory.FactoryBeanWebServiceDirector;

/**
 * 
 * @author danson
 * AbstractHttpInvokerWebServiceFactory.java
 * 2009-9-4
 */
public abstract class AbstractHttpInvokerWebServiceFactory extends AbstractHttpWebServiceFactory {

	public AbstractHttpInvokerWebServiceFactory(String serviceHost) {
		super(serviceHost);
	}

	@Override
	public <T> T getService(Class<T> serviceInterface, String serviceName) {
		return getHttpInvokerService(serviceInterface, serviceName);
	}
	
	@SuppressWarnings("unchecked")
	private <T> T getHttpInvokerService(Class<T> serviceInterface, String serviceName) {
		HttpInvokerProxyFactoryBean proxyFactoryBean = new HttpInvokerProxyFactoryBean();
        proxyFactoryBean.setServiceInterface(serviceInterface);
        proxyFactoryBean.setServiceUrl(constructServiceUrl(serviceName));
        proxyFactoryBean.setHttpInvokerRequestExecutor(createHttpInvokerRequestExecutor());
        return new FactoryBeanWebServiceDirector<HttpInvokerProxyFactoryBean>(proxyFactoryBean)
        		.getWebService();
    }

	private HttpInvokerRequestExecutor createHttpInvokerRequestExecutor() {
		HttpHeaderSupportSimpleHttpInvokerRequestExecutor httpInvokerRequestExecutor = new HttpHeaderSupportSimpleHttpInvokerRequestExecutor(
			httpHeadersProvider
		);
		httpInvokerRequestExecutor.setConnectTimeout(timeout);
		httpInvokerRequestExecutor.setUser(user);
		httpInvokerRequestExecutor.setPassword(password);
		return httpInvokerRequestExecutor;
	}

}
